`
https://leetcode.cn/problems/max-area-of-island/
`

/**
 * @param {number[][]} grid
 * @return {number}
 */
var maxAreaOfIsland = function (grid) {
  const m = grid.length, n = grid[0].length
  let res = 0
  for (let i = 0; i < m; i++) {
    for (let j = 0; j < n; j++) {
      // 遇到陆地，计算岛屿大小并淹没
      if (grid[i][j] === 1) {
        res = Math.max(res, dfs(grid, i, j))
      }
    }
  }

  return res
};

function dfs(grid, i, j) {
  const m = grid.length, n = grid[0].length
  // 边界
  if (i < 0 || j < 0 || i >= m || j >= n) {
    return 0
  }

  // 遇到水
  if (grid[i][j] === 0) {
    return 0
  }

  grid[i][j] = 0

  return dfs(grid, i + 1, j) + dfs(grid, i - 1, j) + dfs(grid, i, j + 1) + dfs(grid, i, j - 1) + 1
}